package client

import (
	"context"
	"google.golang.org/grpc"
	"videos/video-rpc/proto/video"
)

type Handler func(ctx context.Context, in video.VideoClient) (interface{}, error)

func VideoClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8085", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := video.NewVideoClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}
func UserRegister(ctx context.Context, i *video.UserRegisterReq) (*video.UserRegisterResp, error) {
	client, err := VideoClient(ctx, func(ctx context.Context, in video.VideoClient) (interface{}, error) {
		register, err := in.UserRegister(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*video.UserRegisterResp), nil
}

func UserLogin(ctx context.Context, i *video.UserLoginReq) (*video.UserLoginResp, error) {
	client, err := VideoClient(ctx, func(ctx context.Context, in video.VideoClient) (interface{}, error) {
		register, err := in.UserLogin(ctx, i)
		if err != nil {
			return nil, err
		}
		return register, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*video.UserLoginResp), nil
}
